<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace Core\Route;

use Core\Abnormal\Abnormal as Abnormal;
use Core\Func\CoreFunc as CoreFunc;

/**
 * PES路由器
 * @author LazyPHP
 * @version 1.0
 */
class Route {

    /**
     * 初始化路由器规则
     */
    public function index() {
        $this->normal();
        $this->expand();
        define('GROUP', CoreFunc::loadConfig('DEFAULT_GROUP'));
        define('MODULE', 'Index');
        define('ACTION', 'index');
    }

    /**
     * 正常的路由加载模式
     * @throws Abnormal
     */
    private function normal() {
        //获取所有组
        $group = explode(',', CoreFunc::loadConfig('APP_GROUP_LIST'));

        if (!empty($_GET['m']) && !empty($_GET['a'])) {
            //获取组
            try {
                if (empty($_GET['g'])) {
                    define('GROUP', CoreFunc::loadConfig('DEFAULT_GROUP'));
                } elseif (in_array($_GET['g'], $group)) {
                    $key = array_search($_GET['g'], $group);
                    define('GROUP', $group[$key]);
                } else {
                    throw new Abnormal('不存在该项目组');
                }
            } catch (Abnormal $e) {
                echo 'Error Message: ' . $e->getMessage();
                exit;
            }
            define('MODULE', $_GET['m']);
            define('ACTION', $_GET['a']);
            //清空GET
            unset($_GET['g'], $_GET['m'], $_GET['a']);
        }
    }

    /**
     * 扩展模式
     */
    private function expand() {
        //拆分数据
        $routeArray = explode('-', substr($_SERVER['REQUEST_URI'], 1));

        $routeArray[0] = $this->splitIndex($routeArray[0]);

        //拆分组
        $groupList = explode(',', CoreFunc::loadConfig('APP_GROUP_LIST'));

        if (count($routeArray) >= 2) {
            //判断URL首个参数是否存在于用户组
            if (in_array($routeArray[0], $groupList)) {
                define('GROUP', $routeArray[0]);
                define('MODULE', $routeArray[1]);
                define('ACTION', $routeArray[2]);
                unset($routeArray[0], $routeArray[1], $routeArray[2]);
            } else {
                define('GROUP', CoreFunc::loadConfig('DEFAULT_GROUP'));
                define('MODULE', $routeArray[0]);
                define('ACTION', $routeArray[1]);
                unset($routeArray[0], $routeArray[1]);
            }
        }
        //将剩余的非GMA转化为GET参数，以便调用
        $paramArray = array_chunk($routeArray, 2);
        foreach ($paramArray as $key => $value) {
            $_GET[$value[0]] = $value[1];
        }
    }

    /**
     * 当URL存在下级目录时，执行一个替换操作
     * @param type $splitParam 替换的参数
     * @return type 返回替换好的参数
     */
    private function splitIndex($splitParam) {
        //当URL隐藏了index
        if ((substr($_SERVER['SCRIPT_NAME'], 1) != 'index.php') && !strstr($_SERVER['REQUEST_URI'], "index.php")) {
            $splitIndex = substr(str_replace('index.php', "", $_SERVER['SCRIPT_NAME']), 1);
            $result = str_replace($splitIndex, "", $splitParam);
        //当URL没有隐藏index
        } elseif (substr($_SERVER['SCRIPT_NAME'], 1) != 'index.php' || substr($_SERVER['SCRIPT_NAME'], 1) == 'index.php') {
            $splitIndex = substr(str_replace('index.php', "", $_SERVER['SCRIPT_NAME']), 1);
            $result = str_replace(substr($_SERVER['SCRIPT_NAME'], 1) . "/", "", $splitParam);
        }
        return $result;
    }

}
